package com.mars.module.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mars.common.response.PageInfo;
import com.mars.framework.context.ContextUserInfoThreadHolder;
import com.mars.module.admin.entity.SysCat;
import com.mars.module.admin.mapper.SysCatMapper;
import com.mars.module.system.entity.SysUser;
import com.mars.module.system.mapper.SysUserMapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.mars.module.admin.request.SysUserCollectionRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.mars.module.admin.mapper.SysUserCollectionMapper;
import org.springframework.beans.BeanUtils;
import com.mars.module.admin.entity.SysUserCollection;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mars.module.admin.service.ISysUserCollectionService;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户收藏业务层处理
 *
 * @author mars
 * @date 2024-03-30
 */
@Slf4j
@Service
@AllArgsConstructor
public class SysUserCollectionServiceImpl implements ISysUserCollectionService {

    private final SysUserCollectionMapper baseMapper;
    private final SysUserMapper userMapper;
    private final SysCatMapper catMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SysUserCollection add(SysUserCollectionRequest request) {
        SysUserCollection entity = SysUserCollection.builder().build();
        BeanUtils.copyProperties(request, entity);
        Long userId = ContextUserInfoThreadHolder.get().getId();
        entity.setCreateById(userId);
        baseMapper.insert(entity);
        return entity;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(Long id) {
        return baseMapper.deleteById(id) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatch(List<Long> ids) {
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(SysUserCollectionRequest request) {
        SysUserCollection entity = SysUserCollection.builder().build();
        BeanUtils.copyProperties(request, entity);
        return baseMapper.updateById(entity) > 0;
    }

    @Override
    public SysUserCollection getById(Long id) {
        return baseMapper.selectById(id);
    }

    @Override
    public PageInfo<SysUserCollection> pageList(SysUserCollectionRequest request) {
        Page<SysUserCollection> page = new Page<>(request.getPageNo(), request.getPageSize());
        LambdaQueryWrapper<SysUserCollection> query = this.buildWrapper(request);
        IPage<SysUserCollection> pageRecord = baseMapper.selectPage(page, query);
        List<SysUserCollection> records = pageRecord.getRecords();
        List<SysUserCollection> recordsNew = new ArrayList<>();
        for (SysUserCollection collection : records) {
            SysCat sysCat = catMapper.selectById(collection.getCatId());
            SysUser sysUser = userMapper.selectById(collection.getUserId());
            collection.setCatName(sysCat == null ? "" : sysCat.getName());
            collection.setUserName(sysUser == null ? "" : sysUser.getUserName());
            recordsNew.add(collection);
        }
        if (StringUtils.isNotBlank(request.getCatName())) {
            recordsNew = recordsNew.stream().filter(e -> e.getCatName().contains(request.getCatName())).collect(Collectors.toList());
        }
        pageRecord.setRecords(recordsNew);
        return PageInfo.build(pageRecord);
    }


    @Override
    public List<SysUserCollection> list(SysUserCollectionRequest request) {
        LambdaQueryWrapper<SysUserCollection> query = this.buildWrapper(request);
        return baseMapper.selectList(query);
    }

    @Override
    public void deleteByCatId(String id) {
        Long userId = ContextUserInfoThreadHolder.get().getId();
        baseMapper.delete(new QueryWrapper<SysUserCollection>().eq("cat_id", id).eq("user_id", userId));
    }

    private LambdaQueryWrapper<SysUserCollection> buildWrapper(SysUserCollectionRequest param) {
        LambdaQueryWrapper<SysUserCollection> query = new LambdaQueryWrapper<>();
        return query;
    }

}
